﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace The_LarX
{
    public class SphereTrigger : Trigger
    {
        private float maximumDistance;
        public float MaximumDistance
        {
            get { return maximumDistance; }
            set
            {
                maximumDistance = value;
                Actor.Scale = new Vector3(value);
            }
        }

        public BoundingSphere Sphere;

        public SphereTrigger(Renderer renderer, GameScreen gs)
            : base(renderer, gs)
        {
            Sphere = new BoundingSphere();
        }
        public SphereTrigger()
            : base()
        {
            Sphere = new BoundingSphere();
        }

        public override void Draw()
        {
            base.Draw();
            MaximumDistance = (Actor.Scale.X + Actor.Scale.Y + Actor.Scale.Z) / 3;
            Sphere.Center = Actor.Position;
            Sphere.Radius = MaximumDistance;
            this.Inside = Renderer.GetDistance(Actor.Position) <= MaximumDistance;    
            if (Engine.Editor)
            {
                Color Color = Color.Blue;
                if (this.Inside)
                {
                    Color = Color.Yellow;
                }
                Engine.RenderManager.DrawBoundingSphere(Sphere, 32, Color, Renderer.Camera);
            }
        }

        public override bool CheckIntersection(Vector3 obj)
        {
            if (Vector3.Distance(Actor.Position, obj) < maximumDistance)
            {
                return true;
            }
            return false;
        }

        public override void SerializeData()
        {
            base.SerializeData();

            Data.AddData("Trigger.Max", this.MaximumDistance);
        }
        public override void DeserializeData()
        {
            base.DeserializeData();

            this.MaximumDistance = Data.GetData<float>("Trigger.Max");
        }
    }
}
